/************************************************************************************
* YDb.cpp                                                                           *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "YDb.h"


#include <QSqlQuery>

YDb *YDb::__instance = NULL;

YDb::YDb(IDbMaintainer *maintainer,
         IDbRetriever *retriever,
         IDbUpdater *updater) :
    m_maintainer(maintainer),
    m_retriever(retriever),
    m_updater(updater){
}


void YDb::init(IDbMaintainer *maintainer,
               IDbRetriever *retriever,
               IDbUpdater *updater) {
    __instance = new YDb(maintainer, retriever, updater);
    __instance->maintainer()->initDb("yamp.sqlite");
}



YDb* YDb::get() {
    return __instance;
}

const QSqlResult* YDb::executeQuery(QString queryText,
                             QSqlDatabase *database) {
    QSqlQuery* query = NULL;
    if(database != NULL) {
        query = new QSqlQuery();
    }
    else {
        query = new QSqlQuery(*database);
    }
    query->exec(queryText);
    return query->result();
}

IDbMaintainer* YDb::maintainer() {
    return m_maintainer;
}

IDbRetriever* YDb::retriever() {
    return m_retriever;
}

IDbUpdater* YDb::updater() {
    return m_updater;
}
